//
//  JHJViewController.m
//  DayByDay
//
//  Created by dllo on 16/5/20.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "JHJViewController.h"
#import "JHJSongsViewController.h"
#import "JHJSpecialViewController.h"
#import "JHJMVViewController.h"
#import "JHJInfoViewController.h"
#import "STKAudioPlayerHandle.h"

@interface JHJViewController ()<UIScrollViewDelegate,UINavigationControllerDelegate>
@property (nonatomic, strong) JHJSongsViewController *songVC;
@property (nonatomic, strong) JHJSpecialViewController *specialVC;
@property (nonatomic, strong) JHJMVViewController *MVVC;
@property (nonatomic, strong) JHJInfoViewController *infoVC;
@property (nonatomic, assign)NSInteger currentPage;
@property (nonatomic, assign)BOOL isClick;
@property (nonatomic, copy)NSString *value;

@end

@implementation JHJViewController





- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self createView];
    _specialVC.value = self.value;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeValue:) name:@"deliverValue1" object:nil];
}

- (void)changeValue:(NSNotification *)noti {
    self.value = [noti object];
    _specialVC.value = self.value;
    
}

- (void)createView {
    [self createVC];
    
        
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    [self.view addSubview:scrollView];
    scrollView.backgroundColor = [UIColor whiteColor];
    scrollView.delegate = self;
    scrollView.contentSize = CGSizeMake(WIDTH * 3, 0);
    scrollView.tag = 30000;
    [scrollView addSubview:[self.childViewControllers[0] view]];
    _songVC.view.frame = scrollView.frame;
    scrollView.pagingEnabled = YES;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getCurrentPage:) name:@"currentPage" object:nil];
        
    
    
}

- (void)getCurrentPage:(NSNotification *)noti {
    NSLog(@"1111111");
  NSInteger page = [[noti object] integerValue];
    self.isClick = YES;
    if (self.currentPage != page) {
        self.currentPage = page;
        UIScrollView *scrollView = [self.view viewWithTag:30000];
        [scrollView setContentOffset:CGPointMake(WIDTH * page, 0) animated:YES];
        [self scrollViewDidEndDecelerating:scrollView];
    }
    self.isClick = NO;
}
- (void)createVC {
    _songVC = [JHJSongsViewController new];
    
    _specialVC = [JHJSpecialViewController new];
    _MVVC = [JHJMVViewController new];
    _infoVC = [JHJInfoViewController new];
    [self addChildViewController:_songVC];
//    [self.view addSubview:_songVC.view];
    [self addChildViewController:_specialVC];
//    [self.view addSubview:_specialVC.view];
    [self addChildViewController:_MVVC];
//    [self.view addSubview:_infoVC.view];
    [self addChildViewController:_infoVC];
    
//    _songVC.navigationController.delegate = self;
//    _specialVC.navigationController.delegate = self;
//    _MVVC.navigationController.delegate = self;
//    _infoVC.navigationController.delegate = self;
//    _songVC = nil;
//    _specialVC = nil;
//    _MVVC = nil;
//    _infoVC = nil;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    if ([[[self.childViewControllers objectAtIndex:self.currentPage] view] isDescendantOfView:scrollView]) {
        return;
    } else {
//        NSLog(@"======FDSDSAFDSAF");
        UIView *view = [[self.childViewControllers objectAtIndex:self.currentPage] view];
        [scrollView addSubview:[[self.childViewControllers objectAtIndex:self.currentPage] view]];
//        [scrollView bringSubviewToFront:[[self.childViewControllers objectAtIndex:self.currentPage] view]];
        view.frame = CGRectMake(WIDTH * self.currentPage, 0, WIDTH, scrollView.frame.size.width);
        
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if ([[[self.childViewControllers objectAtIndex:(scrollView.contentOffset.x / WIDTH)] view] isDescendantOfView:scrollView]) {
        if (!self.isClick) {
            self.currentPage = scrollView.contentOffset.x / WIDTH;
        }
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"JHJCurrentPage" object:[NSNumber numberWithInteger:self.currentPage]];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"deliverValue1" object:self.value];
        return;
    } else {
        UIView *view = [[self.childViewControllers objectAtIndex:(scrollView.contentOffset.x / WIDTH)] view];
        [scrollView addSubview:[[self.childViewControllers objectAtIndex:(scrollView.contentOffset.x / WIDTH)] view]];
        view.frame = CGRectMake(WIDTH * (scrollView.contentOffset.x / WIDTH), 0, WIDTH, HEIGHT);
        [[NSNotificationCenter defaultCenter] postNotificationName:@"deliverValue1" object:self.value];
    }
    if (!self.isClick) {
        self.currentPage = scrollView.contentOffset.x / WIDTH;
        [[NSNotificationCenter defaultCenter] postNotificationName:@"JHJCurrentPage" object:[NSNumber numberWithInteger:self.currentPage]];
    }


}


-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:_songVC name:@"deliverValue" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:_songVC name:@"firstScrollEnable" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:_specialVC name:@"deliverValue" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:_specialVC name:@"firstScrollEnable" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:_MVVC name:@"deliverValue" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:_MVVC name:@"firstScrollEnable" object:nil];
    
    [[NSNotificationCenter defaultCenter] removeObserver:_infoVC name:@"deliverValue" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:_infoVC name:@"firstScrollEnable" object:nil];
    
//    STKAudioPlayerHandle *stk = [STKAudioPlayerHandle shareSTKAudioPlayerHandle];
//    [stk removeObserver:_songVC forKeyPath:@"currentOfMusic"];
//    [[NSNotificationCenter defaultCenter] removeObserver:self.songVC name:@"MVValue" object:nil];
    self.songVC = nil;
    self.specialVC = nil;
    self.MVVC = nil;
    self.infoVC = nil;
    
//    self.view = nil;
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    NSLog(@"=========dskfdksf");
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
